package com.xzlf;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 自定义一个定时器
 */
public class MyTimer extends Thread {

    private  Long time;

    public MyTimer(Long time){
        this.time = time;
    }

    @Override
    public void run() {
        while (true){
            try {
                TimeUnit.MILLISECONDS.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            print();
        }
    }

    void print(){
        System.out.println("任务执行了：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    void execute(){
        this.start();
    }

    public static void main(String[] args) {
        MyTimer myTimer = new MyTimer(2000L);
        myTimer.execute();
    }
}
